package insight.google.game.era.bo.policy.character;

import insight.google.game.era.bo.GameActionLog;
import insight.google.game.era.bo.GameRoom;
import insight.google.game.era.bo.GameUser;
import insight.google.game.era.bo.card.CharacterCard;
import insight.google.game.era.bo.card.GameCard;
import insight.google.game.era.bo.policy.PolicyCenter;
import insight.google.game.era.client.proxy.PlayerClientProxy;
import insight.google.game.era.client.proxy.PlayerClientProxyImpl;
import insight.google.game.era.core.Util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class LiuBeiPolicy extends CharacterPolicy {

    /**
	 * 
	 */
	private PlayerClientProxy playerClientProxy;

	public LiuBeiPolicy(String id, String name){
		super(id, name);
	}

	@Override
	public boolean beHurtedExec(GameUser original, GameUser[] targets,
			Map params) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean beSelectedExec(GameUser original, GameUser[] targets,
			Map params) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onDiscardExec(GameUser original, GameUser[] targets,
			Map params) {
		// TODO Auto-generated method stub
        playerClientProxy = PlayerClientProxyImpl.getInstance();

		String jsComponentId = (String)params.get("jsComponenetId");
		String policyId = (String)params.get("policyId");
		GameUser targetUser = targets[0];

		GameRoom room = Util.getRoom();
		if("jiJiang".equals(policyId)){
	        Collection<GameUser> userList = room.getUserList();

	        PolicyCenter pCenter = PolicyCenter.getInstance();
	        for(GameUser user : userList){
	        	room.putTask(original, targetUser, PolicyCenter.CARD_TYPE_LIU_BEI_JIJIANG, PolicyCenter.CARD_TYPE_ATTACK);

	            //get match data 
	            Map matchData = (Map)pCenter.getPolicyDataMap(PolicyCenter.CARD_TYPE_LIU_BEI_JIJIANG);
	            String matchMethod = (String)matchData.get(PolicyCenter.KEY_METHOD);

//	            //set target user active
//	            playerClientProxy.setActive(trgUser.getScriptProxy(), jsComponenetId, true);
//	            //let target user do match card
//	            //playerClientProxy.attackCardPolicyExe(trgUser.getScriptProxy(), jsComponenetId, matchMethod, original.getName(), cardName);
//	            playerClientProxy.duelCardPolicyExe(trgUser.getScriptProxy(), jsComponenetId, matchMethod, original.getName(), cardName);
	            targetUser.setClientActive(true, jsComponentId);
	            targetUser.dualCardPolicyExecute("attack", jsComponentId, matchMethod, original.getName());
	        }
		}
		else{
			//set target user active card
	        List<String> activeCards = new ArrayList<String>();
	        activeCards.add("base");
	        activeCards.add("arm");
//	        original.setActiveCards(activeCards);
//	        
//	        playerClientProxy.setActiveCard(original.getScriptProxy(), jsComponenetId, original.getActiveCards());
	        
	        original.setActiveCards(activeCards,jsComponentId);
	        original.enablePolicyExecute(jsComponentId);
//	        playerClientProxy.enablePolicyExec(original.getScriptProxy(), jsComponentId, original);

		}
        return true;
	}

	@Override
	public boolean onTurnExec(GameUser original, GameUser[] targets, Map params) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean withDiscardExec(GameUser original, GameUser[] targets,
			Map params) {
		// TODO Auto-generated method stub
		String jsComponentId = (String)params.get("jsComponenetId");
		String[] cards = (String[])params.get("cardList");
		int cardCount = cards == null?0:cards.length;

        playerClientProxy = PlayerClientProxyImpl.getInstance();
       
        
        //playerClientProxy.setActive(original.getScriptProxy(), jsComponentId, false);
        original.setClientActive(false, jsComponentId);
        
		GameRoom room = Util.getRoom();
		//GameCard card = room.getCardByCardName(cardName);
		//send card to target user
        for(int i=0; i<targets.length; i++){
            GameUser tagUser = targets[i];
            room.sendCardToUserByCards(tagUser, cards);
        }

        if(cardCount >= 2){
            //check user blood
            CharacterCard curCharacter = original.getCharacterCard();
            if(original.getBlood() < curCharacter.getBlood()){
                //heal user life
//                playerClientProxy.healLife(original.getScriptProxy(), jsComponentId, 1);
                original.addBlood(1);
            	original.addBlood(1,jsComponentId);
            }
        }
		//clear client selected card.
//        playerClientProxy.removeClientSelectedCard(original.getScriptProxy(), jsComponentId);
        original.removeClientSelectedCards(jsComponentId);

        List<String> activeCards = new ArrayList<String>();
        activeCards.add("arm");
//        original.setActiveCards(activeCards);
        //playerClientProxy.setActiveCard(original.getScriptProxy(), jsComponentId, original.getActiveCards());
        original.setActiveCards(activeCards,jsComponentId);

//        playerClientProxy.setActive(original.getScriptProxy(), jsComponentId, true);
        original.setClientActive(true, jsComponentId);
        return true;
	}

}
